package com.Lgwd.controller.gpt;

import com.Lgwd.config.GptProperties;
import com.Lgwd.entity.Dialogue;
import com.Lgwd.service.chat.IChatGptService;
import com.Lgwd.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Locale;

/**
 * @author sunchuanchuan
 * @version V1.0
 * @date 2023/3/22
 * @package com.Lgwd.controller.gpt
 * @description
 **/
@RestController
@RequestMapping("/chat/gpt")
@Slf4j
@CrossOrigin
public class ChatGptController {

    @Autowired
    private IChatGptService iChatGptService;

    @Autowired
    private GptProperties gptProperties;

    /**
     * 对话接口
     * @param message
     * {
     * 	"messages":"你好",
     * 	"dialogue_id": 1,
     * 	"user_account":"gh"
     * }
     * @return
     */
    @PostMapping("/dialogue")
    public ResponseData<?> dialogue(@RequestBody Dialogue message){
        try {
            String offending = gptProperties.getOffendingWord().stream().filter(temp -> message.getMessages().toLowerCase(Locale.ROOT).contains(temp.toLowerCase(Locale.ROOT))).findFirst().orElse(null);
            if (!ObjectUtils.isEmpty(offending)) {
                return ResponseData.defaultSuccess("不能包含: "+offending);
            }
            String dialogue = iChatGptService.dialogue(message);
            return ResponseData.ok(dialogue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseData.defaultFail();
    }


    /**
     * 获取key到数据库中
     * @return
     */
    @GetMapping("/getKeyToDataBase")
    public ResponseData<?> getKeyToDataBase() {
        int num = 0;
        do {
            ResponseData<?> keyToDataBase = iChatGptService.getKeyToDataBase();
            if (keyToDataBase.getData() == null) {
                break;
            }
            num++;
        } while (true);
        return ResponseData.ok("本次一共添加了" + num + "条key");
    }

}
